Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

idb

Package Overview
Dependencies
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

idb

A small wrapper that makes IndexedDB usable

  • 7.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.2M
decreased by-6.32%
Maintainers
1
Weekly downloads
 
Created

What is idb?

The idb npm package is a small library that mirrors IndexedDB, but features a more user-friendly API. It allows developers to interact with IndexedDB using promises instead of events, which simplifies the code and improves readability and maintainability. It's particularly useful for applications that need to store large amounts of data on the client side.

What are idb's main functionalities?

Database Creation and Versioning

This code sample demonstrates how to create a new IndexedDB database and an object store within it using idb. The `openDB` function is used to open a database, and it takes the database name, version number, and an upgrade callback as arguments. The upgrade callback is executed when the database is created or upgraded.

const dbPromise = idb.openDB('my-database', 1, {
  upgrade(db) {
    db.createObjectStore('keyval');
  }
});

Adding and Retrieving Data

This code sample shows how to add data to and retrieve data from an object store in IndexedDB using idb. The `addData` function creates a transaction, accesses the object store, and uses the `put` method to add data. The `getData` function retrieves data using the `get` method.

const dbPromise = idb.openDB('my-database', 1);

async function addData(key, val) {
  const db = await dbPromise;
  const tx = db.transaction('keyval', 'readwrite');
  const store = tx.objectStore('keyval');
  store.put(val, key);
  await tx.done;
}

async function getData(key) {
  const db = await dbPromise;
  return db.transaction('keyval').objectStore('keyval').get(key);
}

Cursor Iteration

This code sample illustrates how to iterate over entries in an object store using a cursor. The `iterateCursor` function opens a read-only transaction, accesses the object store, and uses `openCursor` to start the iteration. The cursor is advanced using the `continue` method.

const dbPromise = idb.openDB('my-database', 1);

async function iterateCursor() {
  const db = await dbPromise;
  const tx = db.transaction('keyval', 'readonly');
  const store = tx.objectStore('keyval');
  let cursor = await store.openCursor();
  while (cursor) {
    console.log(cursor.key, cursor.value);
    cursor = await cursor.continue();
  }
}

Other packages similar to idb

FAQs

Package last updated on 04 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc